﻿@{
    Layout = "~/Views/Shared/MainPanel.cshtml";
}

@section HeadContent
{
    <script type="text/javascript" src="@Url.Content("~/Scripts/extjs/ux/Ext.grid.GridPanelToExcel.js")"></script>
    
    <script type="text/javascript">
        var config = {
            title: null,
            pageSize: 99999,
            autoLoad: true,
            remoteSort: false,
            collapsePanel: true,
            exportToExcel: true,
            autoExpandColumn: null,
            searchPanel: null,
            store: null,
            pagingBar: null,
            grid: null,
            showPreview: null,
            showPrevColumn: null,
            viewConfig: null
        };
    </script>
    
    @RenderSection("HeadContent", false)
    
    <script type="text/javascript">
        window.onInitUI_listTemplate = window.onInitUI;
        window.onInitUI = function() {
            if (window.onInitUI_listTemplate) {
                window.onInitUI_listTemplate();
            }
            
            if (gridColumns == null) {
                alert('尚未定义 gridColumns');
                return;
            }
            
            var storeFields = new Array();
            for (var i = 0; i < gridColumns.length; i++) {
                var column = gridColumns[i];
                if (column.dataIndex) {
                    storeFields.push({ name: column.dataIndex, type: column.type });
                }
                if (config.autoExpandColumn == null) {
                    if (column.id && (column.id == "Name" || column.id == "Title" || column.id == "AutoExpand")) {
                        config.autoExpandColumn = column.id;
                    }
                }
                if (column.type == 'float') {
                    if (column.align == null) {
                        column.align = 'right';
                    }
                    if (column.renderer == null) {
                        column.renderer = Ext.util.Format.money;
                    }
                }
                else if (column.type == 'int') {
                    if (column.align == null) {
                        column.align = 'right';
                    }
                }
            }
            if (config.showPreview == true){
                 if (config.showPrevColumn == null) {
                    alert('尚未定义 showPrevColumn');
                    return;
                }else{
                    storeFields.push({ name: config.showPrevColumn });
                }
            }
            
            var store = new Ext.data.JsonStore({
                url: '@(Request.Url)',
                baseParams: {@(ViewData["baseParams"])},
                root: 'rows',
                totalProperty: 'totalCount',
                remoteSort: config.remoteSort,
                fields: storeFields
            });
            if (config.store == null) {
                config.store = store;
            }

            var pagingBarItems = new Array();
            if (config.showPreview == true)
            {
                 pagingBarItems.push(
                    '-',
                    {
                        pressed: true,
                        enableToggle: true,
                        text: '预览',
                        toggleHandler: function(btn, pressed){
                            var view = grid.getView();
                            view.showPreview = pressed;
                            view.refresh();
                        }
                    }
                );
            }
            
            if (config.exportToExcel == true)
            {                
                pagingBarItems.push(
                    '-',
                    {
                        tooltip: '导出为 Excel',
                        icon: '@Url.Content("~/content/extjs/images/default/grid/page_excel.png")',
                        cls: 'x-btn-icon',
                        handler: function() {
                            var title = config.title;
                            if (title == null || title == '') {
                                title = '@(string.IsNullOrEmpty(ViewData["PageTitle"] as string) ? this.Page.Title : Html.Encode(ViewData["PageTitle"]))';
                                if (title == '') {
                                    title = '@("未命名")';
                                }
                            }
                            window.open('data:application/vnd.ms-excel;base64,' + Base64.encode(config.grid.getExcelXml(title)));
                        }
                    }
                );
            }
            
            var items = config.pagingBarItems;
            if (items)
            {
                for (var i = 0, length = items.length; i < length; i++) {
                    pagingBarItems.push(items[i]);
                }
            }

            var pagingBar = new Ext.PagingToolbar({
                pageSize: config.pageSize,
                store: config.store,
                displayInfo: true,
                displayMsg: '显示第 {0} - {1} 条记录 / 共 {2} 条',
                emptyMsg: "没有记录",
                items: pagingBarItems,
                enableOverflow: false // 确保工具条不会被缩起来
            });
            config.pagingBar = pagingBar;

            if (config.title == null || config.title == '') {
                config.title = document.title;
            }
            var grid = new Ext.grid.GridPanel({
                title: config.title,
                store: config.store,
                columns: gridColumns,
                stripeRows: true,
                autoExpandColumn: config.autoExpandColumn,
                autoHeight: true,
                loadMask: true,
                viewConfig: config.viewConfig,
                bbar: pagingBar,
                sm: config.sm
            });
            
            config.grid = grid;
            
            var searchDiv = Ext.get('search');
            if (searchDiv) {
                var formDom = searchDiv.child('form', true);
                if (formDom) {
                    var searchForm = new Ext.form.BasicForm(formDom);
                    if (window.searchForm) {
                        searchForm = window.searchForm;        
                    }
                    //formDom.onsubmit = function() {
                    searchForm.submit = function() {
                        if (!config.grid.rendered) {
                            config.grid.render('resultsCt');
                        }
                        config.store.baseParams = searchForm.getValues();
                        if (config.collapsePanel && config.searchPanel) {
                            config.searchPanel.collapse();
                        }
                        config.store.load({ params: { start: 0, limit: config.pageSize} });
                        return false;
                    };
                }
            }

            if (config.autoLoad == true)
            {
                grid.render('resultsCt');
                if (window.initData) {
                    store.loadData(initData);
                }
                else {
                    store.load({params: { start: 0, limit: config.pageSize }});
                }
            }
        };
    </script>
}

@RenderBody()
    
<div id="resultsCt" style="height: 100%; margin: 5px;"></div>
@if (ViewData.ContainsKey("ListResult"))
{
    <script type="text/javascript">
        window.initData = @Html.Partial(ViewData.ContainsKey("ResultView") ? ViewData["ResultView"].ToString() : "SearchResult", ViewData["ListResult"]);
    </script>
}
   
@RenderSection("BottomContent", false)